package com.example.oppotest.util.youku;

import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;

import java.util.*;

/**
 * @Author wyk
 * @Date: 2025/2/25 22:14
 * CONTENT转化工具
 */
public class MapStrUtil {

    public static String toStr(Map<String, Object> params) {
        if (MapUtils.isEmpty(params)) {
            return null;
        }
        List<String> paramNameList = Lists.newArrayList();
        paramNameList.addAll(params.keySet());
        Collections.sort(paramNameList);
        StringBuilder builder = new StringBuilder();
        boolean first = true;
        for (String key : paramNameList) {
            if (first) {
                first = false;
            } else {
                builder.append("&");
            }
            builder.append(key).append("=");
            Object value = params.get(key);
            String valueString = "";
            if (null != value) {
                if (value instanceof Map) {
                    valueString = String.valueOf(new TreeMap<>((Map) value));
                } else if (value instanceof List) {
                    ArrayList<String> temp = new ArrayList<String>((List) value);
                    Collections.sort(temp);
                    valueString = String.valueOf(temp);
                } else {
                    valueString = String.valueOf(value);
                }
            }
            builder.append(valueString);
        }
        return builder.toString();
    }

}
